/**
 * 抓娃娃主业务逻辑
 */
class Stage extends eui.Component {
	private HOCK_SPEED: number = 800; //钩子下落速度 （毫秒）
	private HOCK_BEGIN: number = 277; //钩子起始位置
	private HOCK_Y: number = 780; //钩子下落到那个位置
	private CSD_SPEED: number = 4; //传送带速度（像素）越小越慢
	private CARAT_TOY: number = 100; //娃娃生产间隔
	private NOW_NUMBER = 15; //计时器的时间,秒
	private DELAYED = 1000; //计时器间隔时间，毫秒


	private csd1: eui.Image; //碰撞体
	private score: eui.Label; //分数面板
	private hock: eui.Image; //钩子
	private xian: eui.Image; //线
	private toysGroup: eui.Group; //玩具动画组
	private countDown: eui.Label; //倒计时数字面板
	private btn: eui.Button; //按钮
	private caughtToyGroup: eui.Group; //被抓住的娃娃对象

	public constructor() {
		super();
		this.skinName = "gameSkin";
		this.init();
		egret.startTick(this.updata, this); //每帧的事件
	}

	//init
	private init() {
		this.btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.touchTap, this) //添加页面点击事件
		this.creatTimeer(); //开启计时器
		this.countDown.text = this.NOW_NUMBER + "";
		var ran = Math.round(Math.random() * 4)
		let toy = new Toy(ran);
		this.toysGroup.addChild(toy);
		//开启倒计时
		this.timer.start();

		// 播放音乐
		// var sound:egret.Sound = RES.getRes("bgMusic_mp3");
		// sound.play();
	}

	//每帧执行的方法
	private updata(): boolean {
		this.creatConnectLine();
		this.collisionDetection();
		this.caughtToyAnimation();
		this.cretaToy();
		return true;
	}

	// 创建娃娃
	private timeer: number = 0;
	private cretaToy() {
		this.timeer++;
		if (this.timeer >= this.CARAT_TOY) {
			this.timeer = 0;
			var ran = Math.round(Math.random() * 4);
			let toy = new Toy(ran);
			this.toysGroup.addChild(toy);
		}

		for (let i = this.toysGroup.numChildren - 1; i >= 0; i--) {
			var toy = < Toy > this.toysGroup.getChildAt(i);
			toy.x -= this.CSD_SPEED;

			if (toy.hitTestPoint(this.hock.x, this.hock.y)) {
				//娃娃的碰撞检测
				toy.x = 0;
				toy.y = 0;
				this.toysGroup.removeChild(toy);
				this.caughtToyGroup.alpha = 1;
				this.caughtToyGroup.addChild(toy);
			}
			if (toy.x < -toy.width) {
				this.toysGroup.removeChild(toy);
			}
		}
	}

	//计时器函数开始	
	private lastTime: number = 0;
	private timer: egret.Timer;
	private creatTimeer() {
		this.timer = new egret.Timer(this.DELAYED, this.NOW_NUMBER);
		this.timer.addEventListener(egret.TimerEvent.TIMER, this.timerFunc, this);
		this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.timerComFunc, this);
	}

	//计时器(每一秒执行)
	private timerFunc() {
		var now = new Date().valueOf();
		this.lastTime = now;
		this.NOW_NUMBER--;
		this.countDown.text = this.NOW_NUMBER + "";
	}

	//计时器函数结束
	private timerComFunc() {
		this.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.touchTap, this) //移除页面点击时间开始进行游戏结束逻辑
		this.gameOver();
	}

	//计时器函数结束
	private gameOver() {
		//禁止按钮
		this.btn.enabled = false;
		//游戏结束的业务逻辑
		var mask: egret.Shape = new egret.Shape(); //遮罩对象

		mask.graphics.beginFill(0x000000);
		mask.graphics.drawRect(0, 0, this.width, this.height);
		mask.alpha = 0.8;
		mask.graphics.endFill();
		this.addChild(mask);

		var ovreTip = new overTipTs();
		ovreTip.x = this.width / 2 - ovreTip.width / 2;
		ovreTip.y = this.height / 2 - ovreTip.height / 2;
		this.addChild(ovreTip);
	}

	//页面点击事件
	private touchTap() {
		if (this.hock.y == this.HOCK_BEGIN) {
			this.btn.enabled = false;
			egret.Tween.get(this.hock).to({
				y: this.HOCK_Y
			}, this.HOCK_SPEED);
			egret.Tween.get(this.caughtToyGroup).to({
				y: this.HOCK_Y
			}, this.HOCK_SPEED);
		}
	}

	//碰撞检测
	private collisionDetection() {
		if (this.csd1.hitTestPoint(this.hock.x, this.hock.y)) {
			egret.Tween.pauseTweens(this.caughtToyGroup);
			egret.Tween.removeTweens(this.caughtToyGroup);
			egret.Tween.get(this.caughtToyGroup).to({
				y: this.HOCK_BEGIN
			}, this.HOCK_SPEED);

			egret.Tween.pauseTweens(this.hock);
			egret.Tween.removeTweens(this.hock);
			egret.Tween.get(this.hock).to({
				y: this.HOCK_BEGIN
			}, this.HOCK_SPEED).call(this.goHome, this);
		}
	}

	//回原位后的处理 此处相关其他业务逻辑
	private goHome() {
		this.btn.enabled = true;
		if (this.caughtToyGroup.numChildren > 0) {
			this.score.text = Number(this.score.text) + 10 + "";
			this.caughtToyGroup.alpha = 0
			this.caughtToyGroup.removeChildren();
		}
	}

	//线的长度跟着爪子走
	private creatConnectLine() {
		this.xian.height = this.hock.y - this.xian.y;
	}

	//被抓住娃娃动画
	private caughtToyAnimation() {
		this.caughtToyGroup.x = this.hock.x;
		this.caughtToyGroup.y = this.hock.y + 120;
	}

	//随机函数
	private betweenMinAndMax(min: number, max: number): number {
		let betwwen = 60;
		return betwwen;
	}
}